<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class LevelService extends BaseService
{
    //会员等级

    protected $types = [
        1 => '黄金|铂金|钻石【自动】', 'SVIP【手动】',
    ];

    /**
     * 列表
     */
    public function getList()
    {
        $db = $this->dbConn($this->dbUlevel)->where('del', 0);
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有门店');
        }
        $types = $this->types;
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            $price = number_format(($v['price'] / 100), 2);
            $tname = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'ver' => $v['ver'],
                'icon' => file_exists($v['icon']) ? env('DOMAIN') . '/' . $v['icon'] : '',
                'describe' => $v['describe'],
                'price' => $price,
                'type' => $v['type'],
                'tname' => $tname,
                'real_cost' => $v['real_cost'] / 100,
                'discount' => $v['discount'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    /**
     * 一个
     * @param int $id 门店ID
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbUlevel)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有等级');
        }
        $res['price'] = number_format(($res['price'] / 100), 2);
        $res['real_cost'] = number_format(($res['real_cost'] / 100), 2);
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }

    /**
     * 添加
     * @param array $pas
     */
    public function addOne($pas)
    {
        $resOne = (array) $this->dbConn($this->dbUlevel)
            ->where('del', 0)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($resOne)) {
            return array('code' => -2, 'msg' => '等级有重复');
        }
        $pas['price'] = $pas['price'] * 100;
        $pas['real_cost'] = $pas['real_cost'] * 100;
        $pas['addtime'] = $pas['uptime'] = toDate();
        $sid = $this->dbConn($this->dbUlevel)->insertGetId($pas);
        if (!$sid) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code'=>0, 'msg'=>'success');
//        DB::beginTransaction(); //开启事务
//        try {
//            DB::commit(); //提交事务
//        } catch (\Exception $e) {
//            DB::rollBack(); //事务回滚
//            echo $e->getMessage();
//            return array('code'=>-1, 'msg'=>'操作失败');
//        }
    }

    /**
     * 修改
     * @param int $id 门店ID
     * @param array $pas
     */
    public function modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbUlevel)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '等级不存在');
        }
        $pas['price'] = $pas['price'] * 100;
        $pas['real_cost'] = $pas['real_cost'] * 100;
        $pas['uptime'] = toDate();
        $sup = $this->dbConn($this->dbUlevel)->where('id', $id)->update($pas);
        if (!$sup) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code'=>0, 'msg'=>'success');
    }

    /**
     * 删除
     * @param int $id 商品ID
     */
    public function setDel($id)
    {
        $res = (array) $this->dbConn($this->dbUlevel)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '等级不存在');
        }
        $arys = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = $this->dbConn($this->dbUlevel)->where('id', $id)->update($arys);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }
}